//Java 16+ 特性
public class Record {
    //编译：
    // PS C:\Users\ASUS\Desktop\JavaCode\test> D:\JAVA-jdk\jdk17.0.5\bin\javac.exe -encoding UTF-8 Record.java
    //反编译
    // PS C:\Users\ASUS\Desktop\JavaCode\test> D:\JAVA-jdk\jdk17.0.5\bin\javap.exe  Innerrecord.class
    // PS C:\Users\ASUS\Desktop\JavaCode\test> D:\JAVA-jdk\jdk17.0.5\bin\javap.exe  Ir1.class
    // PS C:\Users\ASUS\Desktop\JavaCode\test> D:\JAVA-jdk\jdk17.0.5\bin\javap.exe  Ir2.class
    public static void main(String[] args) {
        Innerrecord innerrecord = new Innerrecord(0, 0);
        System.out.println(Innerrecord.c);
        System.out.println(innerrecord.a());
        innerrecord.out();

        Ir1 ir1 = new Ir1(2, 0);
        Ir2 ir2 = new Ir2();
        Ir2 ir2_1 = new Ir2(12, 12);

    }
}

record Innerrecord(int a, int b) // 就是构造器，a,b自动变成private final修饰的变量
{
    /*
    * 反编译结果：
    * final class Innerrecord extends java.lang.Record {
      static int c;
      Innerrecord(int, int);
      public void out();
      public final java.lang.String toString();
      public final int hashCode();
      public final boolean equals(java.lang.Object);
      public int a();
      public int b();
      static {};
    }

    *
    * */
    static int c = 10;//只能定义static变量

    public void out() {
        System.out.println(a + "_" + b + "_" + c);
    }

}

record Ir1(int a, int b) {
    /*
    * 反编译结果：
    * final class Ir1 extends java.lang.Record {
      static int c;
      public Ir1(int, int);
      public void out();
      public final java.lang.String toString();
      public final int hashCode();
      public final boolean equals(java.lang.Object);
      public int a();
      public int b();
      static {};
    }

    * */
    // 紧凑构造器：参数与组件同名，编译器会自动把参数赋值给对应字段
    public Ir1 {
        if (a == 10) {
            System.out.println("a为什么要是10？");
        }
    }

    static int c = 10;

    public void out() {
        System.out.println(a + "_" + b + "_" + c);
    }

}

record Ir2(int a, int b) {
    /*
    * 反编译
    * final class Ir2 extends java.lang.Record {
      static int c;
      public Ir2();
      public Ir2(int, int);
      public void out();
      public final java.lang.String toString();
      public final int hashCode();
      public final boolean equals(java.lang.Object);
      public int a();
      public int b();
      static {};
    }

    * */
    public Ir2() {
        // 无参构造器，必须调用 this(...)
        this(2, 3);
    }

    public Ir2(int a, int b) {
       this.a=a;
       this.b=b;
        if (a == 10) {
            System.out.println("a为什么要是10？");
        }
    }

    static int c = 10;

    public void out() {
        System.out.println(a + "_" + b + "_" + c);
    }

}